package repository.relsys;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;

import java.util.Map;

import org.apache.log4j.Logger;

import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.util.FileManager;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;

import repository.relsys.app.Settings;
import repository.relsys.config.Property;
import repository.relsys.domainmodel.ltool.interfaces.Blog;
import repository.relsys.domainmodel.ltool.interfaces.CollaborativeTagging;
import repository.relsys.domainmodel.ltool.interfaces.ContactList;
import repository.relsys.domainmodel.ltool.interfaces.Feature;
import repository.relsys.domainmodel.ltool.interfaces.GeneralInformation;
import repository.relsys.domainmodel.ltool.interfaces.InstantMessaging;
import repository.relsys.domainmodel.ltool.interfaces.LearningTool;
import repository.relsys.domainmodel.ltool.interfaces.MobileDevice;
import repository.relsys.domainmodel.ltool.interfaces.MultimediaHosting;
import repository.relsys.domainmodel.ltool.interfaces.OperetingSystem;
import repository.relsys.domainmodel.ltool.interfaces.SiteSearch;
import repository.relsys.domainmodel.ltool.interfaces.SocialNetworking;
import repository.relsys.domainmodel.ltool.interfaces.TechnicalInformation;
import repository.relsys.domainmodel.ltool.interfaces.ToolAvailability;
import repository.relsys.domainmodel.ltool.interfaces.ToolCost;
import repository.relsys.domainmodel.ltool.interfaces.UserProfileManagement;
import repository.relsys.domainmodel.ltool.pojos.LearningToolCls;
import repository.relsys.domainmodel.ltool.pojos.MobileDeviceCls;
import repository.relsys.domainmodel.ltool.pojos.OperetingSystemCls;
import repository.relsys.domainmodel.ltool.pojos.ToolAvailabilityCls;
import repository.relsys.domainmodel.ltool.pojos.ToolCostCls;

import repository.relsys.services.DataModelManager;

import repository.relsys.services.RelsysReadWrite;
import repository.relsys.services.datacentral.FileDataProvider;
import repository.relsys.services.spring.RelsysResourceFactory;
import repository.relsys.util.json.JSONExporter;
import repository.relsys.util.json.JSONExporterCls;
import thewebsemantic.Bean2RDF;
import thewebsemantic.RDF2Bean;

public class RelsysTest3 {

	private static Logger logger = Logger
			.getLogger(RelsysTest3.class.getName());

	public static void main(String[] args) {
		try {

			Settings.getInstance();

			RelsysTest3.initRepository();
			logger.info("Repository initialised!");

			System.in.read();

			DataModelManager.getInstance().flush();

		} catch (Exception e) {
			logger.error(e.getMessage());
		}
	}

	public static void initRepository() throws Exception {

		//importInstances();
	

		Collection<LearningTool> ltools = new LinkedList<LearningTool>();

		RelsysResourceFactory rf = new RelsysResourceFactory();

		LearningTool ltool1 = (LearningTool) rf
				.createResource(LearningTool.class);
		ltool1.setTitle("WordPress");
		ltool1.setToolImgURL("http://upload.wikimedia.org/wikipedia/commons/thumb/c/ca/Wordpress-logo.png/250px-Wordpress-logo.png");

		GeneralInformation gi1 = (GeneralInformation) rf
				.createResource(GeneralInformation.class);

		gi1
				.setDescription("WordPress is an open source blogging platform that is free and easy to use.");
		gi1.setToolURL("http://www.wordpress.com");
		ToolCost tc = (ToolCost) rf.createResource(ToolCost.class);
		ToolAvailability ta = (ToolAvailability) rf
				.createResource(ToolAvailability.class);
		tc.setTitle("Free");
		ta.setTitle("Hosted");

		gi1.setToolCost(tc);
		gi1.setToolAvailability(ta);

		ltool1.setGeneralInfo(gi1);

		TechnicalInformation ti1 = (TechnicalInformation) rf
				.createResource(TechnicalInformation.class);
		ti1.registration(true);
		ti1.setLaunchedTime("12-01-2001");
		ti1.setVersion("2.9");

		ti1.isOpenSource(false);

		MobileDevice mdCls1 = (MobileDevice) rf
				.createResource(MobileDevice.class);
		mdCls1.setTitle("Blackberry");

		MobileDevice mdCls2 = (MobileDevice) rf
				.createResource(MobileDevice.class);
		mdCls2.setTitle("IPhone");
		MobileDevice mdCls3 = (MobileDevice) rf
				.createResource(MobileDevice.class);
		mdCls3.setTitle("Andorid");
		ti1.addMD(mdCls1);
		ti1.addMD(mdCls2);
		ti1.addMD(mdCls3);

		OperetingSystem os1 = (OperetingSystem) rf
				.createResource(OperetingSystem.class);
		os1.setTitle("Linux");
		OperetingSystem os2 = (OperetingSystem) rf
				.createResource(OperetingSystem.class);
		os2.setTitle("Windows");
		OperetingSystem os3 = (OperetingSystem) rf
				.createResource(OperetingSystem.class);
		os3.setTitle("MacOS");

		ti1.addOS(os1);
		ti1.addOS(os2);
		ti1.addOS(os3);
		/*
		 * ti1.addMD(MobileDeviceCls.BLACKBERRY);
		 * ti1.addMD(MobileDeviceCls.ANDROID);
		 * ti1.addMD(MobileDeviceCls.IPHONE);
		 * ti1.addOS(OperetingSystemCls.LINUX);
		 * ti1.addOS(OperetingSystemCls.WINDOWS);
		 * ti1.addOS(OperetingSystemCls.MAC);
		 */

		ltool1.setTechnicalInfo(ti1);

		Collection<Feature> features = new LinkedList<Feature>();
		Blog blog = (Blog) rf.createResource(Blog.class);
		ContactList folowers = (ContactList) rf
				.createResource(ContactList.class);
		blog.setTitle("Blog");
		folowers.setTitle("ContactList");
		features.add(blog);
		features.add(folowers);

		ltool1.setFeatures(features);

		

		ltools.add(ltool1);
		//ltools.add(ltool2);
		
		//export(ltool1, "tools", "TURTLE");
		//export(ltool2, "tools", "TURTLE");
		LearningTool p =importLT("tools", "TURTLE");
         export(p, "im", "RDF/XML");
	}

	public static void importInstances() {

		// RelsysResourceFactory rf = new RelsysResourceFactory();

		OntModel dataModel = ModelFactory.createOntologyModel();

		dataModel.read("http://diplomski.nelakolundzija.org/individuals.rdf",
				"RDF/XML");

		ExtendedIterator<OntClass> it = dataModel.listClasses();
		while (it.hasNext()) {
			OntClass cls = it.next();
			System.out.println(cls.getURI());
		}

		/*
		 * RDF2Bean reader = new RDF2Bean(dataModel); Collection<LearningTool>
		 * ltools =
		 * reader.loadDeep(rf.getBeanImplementationClass(LearningTool.class));
		 * 
		 * LinkedList<LearningTool> list = (LinkedList<LearningTool>)ltools;
		 * 
		 * Iterator<LearningTool> iter = list.iterator();
		 * 
		 * while (iter.hasNext()) { LearningTool t = (LearningTool) iter.next();
		 * System.out.println(t.getTitle()); } return ltools;
		 */

	}
	
	
	 public static void export(LearningTool ltool, String fileName, String syntax){
		 Model dataModel = null;
         try {
        	 dataModel = ModelFactory.createDefaultModel();
 			dataModel.setNsPrefix("lt",
 					"http://diplomski.nelakolundzija.org/LTontology.rdf#");
 			dataModel.setNsPrefix("swc",
 					"http://data.semanticweb.org/ns/swc/ontology#");
 			dataModel.setNsPrefix("jenabean", "http://thewebsemantic.com/");
 			dataModel.setNsPrefix("dc", "http://purl.org/dc/elements/1.1/");
 			dataModel.setNsPrefix("xsd", "http://www.w3.org/2001/XMLSchema#");
 			dataModel.setNsPrefix("rdfs",
 					"http://www.w3.org/2000/01/rdf-schema#");

                 Bean2RDF writer = new Bean2RDF(dataModel);
                 
                 writer.saveDeep(ltool);
                 dataModel.write(new FileOutputStream(fileName), syntax);
                 
         } catch (Exception e) {
                 System.err.println("Error in writting");
                 e.printStackTrace();
         }
 }


	


	public static LearningTool importLT(String fileName,
			String syntax) {
		
		LearningTool ltool = null;
	
	
		try {
			
		    InputStream is = new BufferedInputStream(new FileInputStream(fileName));
            Model dataModel  = ModelFactory.createDefaultModel().
                    read(new InputStreamReader(is), "http://diplomski.nelakolundzija.org/LTontology.rdf#", syntax);
          
		  is.close();
		
			RDF2Bean reader = new RDF2Bean(dataModel);
		 ltool = reader.loadDeep(LearningTool.class,0);

		
			
		} catch (Exception e) {
			logger.error("Could not load DataModel from file: " + e);
		}

	

		
		return ltool;

	}

}
